package com.ruyuan.eshop.manager.service.impl;

import com.ruyuan.eshop.framework.utils.JWTUtil;
import com.ruyuan.eshop.manager.config.ManagerLoginUser;
import com.ruyuan.eshop.manager.domain.dto.UserAccountDTO;
import com.ruyuan.eshop.manager.domain.request.UserLoginRequest;
import com.ruyuan.eshop.manager.service.AccountManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 用户管理Service组件
 * </p>
 *
 * @author pwd
 * @version 1.0
 */
@Slf4j
@Service
public class AccountManagerServiceImpl implements AccountManagerService {

    /**
     * 用户登录
     * @param userLoginRequest 账号密码信息
     * @return token
     */
    @Override
    public String login(UserLoginRequest userLoginRequest) {
        // 入参检查
        String username = userLoginRequest.getUsername();
        String password = userLoginRequest.getPassword();
        Assert.notNull(username, "用户名不能为空");
        Assert.notNull(password, "密码不能为空");

        // 用户名与密码校验  TODO
        // 这里默认都能登录成功

        // 封装token
        ManagerLoginUser loginUser = new ManagerLoginUser(1L, username);
        String token = JWTUtil.geneJsonWebToken(loginUser);
        return token;
    }

    @Override
    public UserAccountDTO detail(String username) {
        Assert.notNull(username, "用户名不能为空");
        // 返回模拟数据
        UserAccountDTO userAccount = new UserAccountDTO();
        userAccount.setUsername(username);
        userAccount.setSex(1);
        userAccount.setPhone("13512345678");
        userAccount.setMail("demo@ruyuan.com");
        userAccount.setHeadImg(null);
        return userAccount;
    }
}